package com.example.spring_mybatis.controller;

import com.alibaba.fastjson.JSON;
import com.example.spring_mybatis.utils.CopyUtil;
import com.example.spring_mybatis.utils.RUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.example.spring_mybatis.vo.PageResponseVO;
import com.example.spring_mybatis.service.IRoleService;
import com.example.spring_mybatis.model.RoleEntity;
import com.example.spring_mybatis.dto.RoleDTO;
import com.example.spring_mybatis.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import java.util.Map;



/**
 * <p>
 * 角色表(也可以当部门表) 前端控制器
 * </p>
 *
 * @作者: 水痕
 * @时间: 2023-09-17 21:55:23
 * @描素: 角色表(也可以当部门表)
 */
@RestController
@RequestMapping("/role")
@Slf4j
@Validated
public class RoleController {
    @Autowired
    private IRoleService roleService;

    /**
    * 创建-角色表(也可以当部门表)
    */
    @PostMapping()
    public RUtil createApi(
        @RequestBody @Validated RoleDTO RoleDTO
    ) {
        return this.roleService.createApi(RoleDTO);
    }

    /**
    * 根据id删除-角色表(也可以当部门表)
    */
    @DeleteMapping("{id}")
    public RUtil<String> deleteByIdApi(@PathVariable @Valid Integer id) {
        return this.roleService.deleteByIdApi(id);
    }

    /**
    * 根据id修改-角色表(也可以当部门表)
    */
    @PutMapping("{id}")
    public RUtil<String> modifyByIdApi(
        @PathVariable @Valid Integer id,
        @RequestBody @Validated RoleDTO RoleDTO
    ) {
        return this.roleService.modifyByIdApi(id, RoleDTO);
    }

    /**
    * 分页获取列表
    */
    @GetMapping
    public RUtil<PageResponseVO<RoleVO>> getDataPageApi(
        @RequestParam(required = false, defaultValue = "1", name = "pageNumber")
        @Min(value = 1, message = "pageNumber最小为1") Integer pageNumber,
        @RequestParam(required = false, defaultValue = "10", name = "pageSize")
        @Min(value = 1, message = "pageSize最小为1") Integer pageSize
    ) {
        Map<String, Object> resultData = this.roleService.getDataPageApi(pageNumber,pageSize);
        PageResponseVO<RoleVO> result = new PageResponseVO<>();
        result.setList(JSON.parseArray(JSON.toJSONString(resultData.get("list")), RoleVO.class));
        result.setPageNumber(pageNumber);
        result.setPageSize(pageSize);
        result.setTotal((Long) resultData.get("total"));
        return RUtil.success(result);
    }

    /**
    * 根据id获取单条数据
    */
    @GetMapping("{id}")
    public RUtil<RoleVO> getByIdApi(@PathVariable @Valid Integer id) {
        RoleEntity Role = this.roleService.getByIdApi(id);
//        RoleVO RoleVO = new RoleVO();
//        BeanUtils.copyProperties(Role,RoleVO);
        RoleVO roleVO = CopyUtil.copy(Role, RoleVO.class);
        return RUtil.success(roleVO);
    }
}